package com.aso114.project.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.base.BaseApplication;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by asus on 2018/11/7.
 */

public class GridAdapter extends BaseAdapter {

    private int[] imageRes;
    private String[] name;

    public GridAdapter(int[] imageRes, String[] name) {
        this.imageRes = imageRes;
        this.name = name;
    }

    @Override
    public int getCount() {
        return imageRes.length;
    }

    @Override
    public Object getItem(int i) {
        return imageRes[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(BaseApplication.getInstance().getBaseContext()).inflate(R.layout.needkownitem, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        //名称
        holder.tvName.setText(TextUtils.isEmpty(name[position]) ? "" : name[position]);
        //图片
        holder.ivIcon.setImageResource(imageRes[position]);
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.iv_icon)
        ImageView ivIcon;
        @BindView(R.id.tv_name)
        TextView tvName;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
